<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\dctrl;

use yii\helpers\ArrayHelper;
use builder\form\util\control\Control;

/**
 * 动态控件
 * @author cleverstone
 * @since ym2.0
 */
class DynamicControl extends Control
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'dynamic_control',
        'label' => '',
        'comment' => '',
        '__default' => [],//默认值
        '__valTpl' => [],//值模板
        'items' => [],
        'data' => [],
        'allowDefaultDrop' => false,//允许删除默认行
        'allowDragSorting' => false,//是否允许拖拽排序
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',
        'cellMinWidth' => '80px',
        'skinAttribute' => ['lay-skin' => 'nob', 'lay-size' => 'sm'],//表格皮肤属性
    ];

    /**
     * 表格皮肤
     * @param array $attribute
     * - lay-skin=grid 全边框
     * - lay-skin=line 行边框表格
     * - lay-skin=row 列边框表格
     * - lay-skin=nob 无边框
     *
     * - lay-size=sm 小尺寸
     * - lay-size=lg 大尺寸
     * - lay-even="" 偶数行背景色
     *
     * @return $this
     */
    public function skinAttribute(array $attribute)
    {
        $attribute = ArrayHelper::merge($this->_options['skinAttribute'], $attribute);
        $this->formatHtmlAttribute($attribute);
        $this->_options['skinAttribute'] = $attribute;

        return $this;
    }

    /**
     * 是否允许拖拽排序
     * @return $this
     */
    public function allowDragSorting()
    {
        $this->_options['allowDragSorting'] = true;
        return $this;
    }

    /**
     * 允许删除默认行
     * @return $this
     */
    public function allowDefaultDrop()
    {
        $this->_options['allowDefaultDrop'] = true;
        return $this;
    }

    /**
     * 单元格最小宽度
     * @param integer|string $cellMinWidth
     * @return $this
     */
    public function cellMinWidth($cellMinWidth)
    {
        if (is_numeric($cellMinWidth)) {
            $cellMinWidth .= 'px';
        }

        $this->_options['cellMinWidth'] = $cellMinWidth;
        return $this;
    }

    /**
     * 控件标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 默认数据
     * @param array $defaultData
     * @return $this
     */
    public function data(array $defaultData)
    {
        $this->_options['data'] = $defaultData;
        return $this;
    }

    /**
     * 动态控件项
     * @param array $items
     * @return $this
     */
    public function items(array $items)
    {
        $this->_options['items'] = $items;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->_options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        $options = $this->_options;
        if (is_array($options['skinAttribute'])) {
            $this->formatHtmlAttribute($options['skinAttribute']);
        }

        return $options;
    }
}